package com.yanqu.road.server.cmd.activity.entername;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityConfig;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameCache;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ENTER_NAME_ADD, desc = "报名")
public class EnterNameAddCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        EnterNameProto.EnterNameAddReqMsg reqMsg = EnterNameProto.EnterNameAddReqMsg.parseFrom(packet.getBytes());
        if(!EnterNameActivityMgr.inEnterNameTime(reqMsg.getActivityId())){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        //是否系统解锁
        EnterNameActivityConfig config = EnterNameActivityMgr.getActivityConfig(reqMsg.getActivityId());
        if(!EnterNameActivityMgr.isActivitySystemOpen(player, config.getActivityInfo())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        EnterNameCache cache = EnterNameActivityMgr.getEnterNameCache(reqMsg.getActivityId(), packet.getPlayerId());
        if(cache != null){
            ret = GameErrorCode.E_ENTER_NAME_HAD_ADD;
            return;
        }


        //分区校验
        int zoneId = reqMsg.getZoneId();
//        EnterNameActivityConfig config = EnterNameActivityMgr.getActivityConfig(reqMsg.getActivityId());
        if(config.getChannelFightZone(zoneId) == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        EnterNameProto.ChannelEnterNameAddReqMsg.Builder channelReq = EnterNameProto.ChannelEnterNameAddReqMsg.newBuilder();
        channelReq.setZoneId(reqMsg.getZoneId());
        channelReq.setUserKey(EnterNameActivityMgr.buildKeyMsg(player.getUserId()));
        UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId());
        channelReq.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        channelReq.setActivityId(reqMsg.getActivityId());
        player.sendChannelPacket(ChannelProtocol.N_ENTER_NAME_ADD, channelReq);

    }

    @Override
    public void sendData(GamePlayer player) {

        if(ret != 0){
            player.sendPacket(Protocol.U_ENTER_NAME_ADD, EnterNameProto.EnterNameAddRespMsg.newBuilder().setRet(ret));
        }

    }
}
